// var symptomName = last_month_day();
// var prefix = ctx;

// 新增病人记录
const x = new Array();
const y = new Array();
function staticClinicDeptadd(){
    $.post("/system/pat_master_index/selectMonthCount",function (ppData){
        if(ppData!=null){
            const data = ppData;
            const code = data.code;
            const rows = data.rows;
            const total = data.total;
            if(code!=0){
                alert("统计失败！");
            }else{
                for (let i=0;i<data.rows.length;i++){
                    let month = rows[i].time;
                    let count = rows[i].number;
                    x.push(month);
                    y.push(count);
                }
                const addPatient = echarts.init(document.getElementById('addPatient'));
                addPatient.setOption({
                    color: ['#87cefa'],
                    grid: {
                        left: '5%',
                        right: '5%',
                        bottom: '5%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a}<br/>{b}<br/>{c}人"
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            data: x,
                            axisLine: {
                                lineStyle: {
                                    color: '#87cefa'
                                },
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 40,
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 13
                                }
                            }
                        }
                    ],
                    yAxis: [
                        {
                            show: false,
                            type: 'value',
                            axisLine: {
                                lineStyle: {
                                    color: '#87cefa'
                                },
                            },
                            splitLine: {
                                "show": false
                            },
                            axisLabel: {
                                textStyle: {
                                    color: '#fff'
                                },
                                formatter: function (value) {
                                    return value + "人"
                                },
                            },
                        }
                    ],
                    series: [
                        {
                            name: '每月新增病人',
                            type: 'bar',
                            barWidth: 30,
                            data: y,
                        },
                    ]
                });
                // console.log(x);
                // console.log(y);
            }
        }
    });
}

//历年等床病人数
const wait_data= [];
function selectWaitBedYear(){
    $.post("/system/waitbedpats/selectWaitBedYear",function (ppData){
        if(ppData!=null){
            const data = ppData;
            const code = data.code;
            const rows = data.rows;
            const total = data.total;
            if(code!=0){
                alert("统计失败！");
            }else{
                for (let i=0;i<data.rows.length;i++){
                    let deptname = rows[i].年份;
                    wait_data.push(deptname);
                }
                rows.map((item,index) => {
                    rows[index]={
                        value : item.人数,
                        name : item.年份
                    }
                })
                const histogramChart1 = echarts.init(document.getElementById('histogramChart1'));
                histogramChart1.setOption({
                    color: ["#87cefa", "#ff7f50", "#32cd32", "#da70d6",],
                    legend: {
                        y: '240',
                        x: 'center',
                        textStyle: {
                            color: '#ffffff',

                        },
                        data: wait_data,
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{b}年等床病人数<br/>{c}人 <br/>占比：{d}%"
                    },
                    calculable: false,
                    series: [
                        {
                            name: '等床病人人数',
                            type: 'pie',
                            radius: ['40%', '70%'],
                            center: ['50%', '45%'],
                            itemStyle: {
                                normal: {
                                    label: {
                                        show: false
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                },
                                emphasis: {
                                    label: {
                                        show: true,
                                        position: 'center',
                                        textStyle: {
                                            fontSize: '20',
                                            fontWeight: 'bold'
                                        }
                                    }
                                }
                            },
                            data: rows
                        }
                    ]
                });
            }
        }
    });

}

// 科室就诊人数(人)
const static_data = [];
const static_num = [];
function staticClinicDept(){
    $.post("/system/clinicmaster/staticclinicdept",function (ppData){
        if(ppData!=null){
            const data = ppData;
            const code = data.code;
            const rows = data.rows;
            const total = data.total;
            if(code!=0){
                alert("统计失败！");
            }else{
                if (data.rows.length<10) {
                    for (let i=0;i<data.rows.length;i++){
                        let deptname = rows[i].DEPT_NAME;
                        let count = rows[i].num;
                        static_data.push(deptname);
                        static_num.push(count);
                    }
                } else if(data.rows.length>10) {
                    for (let i=0;i<10;i++){
                        let deptname = rows[i].DEPT_NAME;
                        let count = rows[i].num;
                        static_data.push(deptname);
                        static_num.push(count);
                    }
                }

                const histogramChart4 = echarts.init(document.getElementById('histogramChart4'));
                histogramChart4.setOption({
                    color: ['#87cefa'],
                    grid: {
                        left: '5%',
                        right: '5%',
                        bottom: '5%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a}<br/>{b}<br/>{c}人"
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            data: static_data,
                            axisLine: {
                                lineStyle: {
                                    color: '#87cefa'
                                },
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 40,
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 13
                                }
                            }
                        }
                    ],
                    yAxis: [
                        {
                            show: false,
                            type: 'value',
                            axisLine: {
                                lineStyle: {
                                    color: '#87cefa'
                                },
                            },
                            splitLine: {
                                "show": false
                            },
                            axisLabel: {
                                textStyle: {
                                    color: '#fff'
                                },
                                formatter: function (value) {
                                    return value + "人"
                                },
                            },
                        }
                    ],
                    series: [
                        {
                            name: '科室就诊人数',
                            type: 'bar',
                            barWidth: 30,
                            data: static_num,
                        },
                    ]
                });

            }
        }
    });
}

// 就诊人员类型分布
// const dentity_data = [];
function staticDentity(){
    $.post("/system/clinicmaster/staticclinicidentity",function (ppData){
        if(ppData!=null){
            var data = ppData;
            var code = data.code;
            var rows = data.rows;
            var total = data.total;
            if(code!=0){
                alert("统计失败！");
            }else{
                rows.slice(0,7).map((item,index) => {
                    rows[index]={
                        value : item.num,
                        name : item.identity
                    }
                })
                // dentity_data = rows.slice(0,10)
                const patientType = echarts.init(document.getElementById('patientType'));
                patientType.setOption({
                    color: ["#87cefa", "#ff7f50", "#32cd32", "#da70d6",],
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a}<br/>{b}<br/>{c}人"
                    },
                    calculable: true,
                    series: [
                        {
                            name: '就诊人员类型分布',
                            type: 'pie',
                            radius: [30, 110],
                            center: ['45%', '50%'],
                            roseType: 'area',
                            x: '50%',
                            max: 40,
                            sort: 'ascending',
                            data: rows
                        }
                    ]
                })
            }
        }
    });
}

// 科室出入院数据统计
const  goin_data = [];
const  gonum_data = [];
// const  innum_data = [];
function goinHospital(){
    $.post("/system/patvisit/countpatvisitbyadmission",function (ppData){
        if(ppData!=null){
            const data = ppData;
            const code = data.code;
            const rows = data.rows;
            const total = data.total;
            if(code!=0){
                alert("统计失败！");
            }else{
                for (let i=0;i<10;i++){
                    let month = rows[i].DEPT_NAME;
                    let count = rows[i].dept_admission_to_count;
                    goin_data.push(month);
                    gonum_data.push(count);
                }
                const pieChart1 = echarts.init(document.getElementById('pieChart1'));
                pieChart1.setOption({
                    title: {
                        text: 'Stacked Line',
                        show: false
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: goin_data,
                        axisLabel: {
                            show: true,
                            interval: 0,
                            rotate: 40,
                            textStyle: {
                                color: '#fff'
                            }
                        }
                    },
                    yAxis: {
                        type: 'value',
                        show: false
                    },
                    series: [
                        {
                            name: '入院人数',
                            type: 'line',
                            stack: 'Total',
                            data: gonum_data
                        }
                    ]
                });
           }
        }
    });



}

// 住院军种类型人员统计
const myColor = ['#1089E7', '#F57474', '#56D0E3', '#F8B448', '#8B78F6'];
const soldier_name = [];
const soldier_num =[];
function soldierType(){
    $.post("/system/patvisit/countatmedservices",function (ppData){
        if(ppData!=null){
            var data = ppData;
            var code = data.code;
            var rows = data.rows;
            var total = data.total;
            if(code!=0){
                alert("统计失败！");
            }else{
                for (let i=0;i<data.rows.length;i++){
                    // console.log(rows[i].name)
                    let month = rows[i].name;
                    let count = rows[i].armed_services_count;
                    soldier_name.push(rows[i].name);
                    soldier_num.push(count);
                }

                const rescueNum = echarts.init(document.getElementById('rescueNum'));
                rescueNum.setOption({
                    grid: {
                        top: '20%',
                        left: '32%'
                    },
                    xAxis: {
                        show: false
                    },
                    yAxis: [{
                        show: true,
                        data:  soldier_name,
                        inverse: true,
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            show: false
                        },
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            color: '#fff',
                            formatter: (value, index) => {
                                return [

                                    `{lg|${index+1}}  ` + '{title|' + value + '} '
                                ].join('\n')
                            },
                            rich: {
                                lg: {
                                    backgroundColor: '#339911',
                                    color: '#fff',
                                    borderRadius: 15,
                                    // padding: 5,
                                    align: 'center',
                                    width: 15,
                                    height: 15
                                },
                            }
                        },


                    }, {
                        show: true,
                        inverse: true,
                        data: soldier_num,
                        axisLabel: {
                            textStyle: {
                                fontSize: 12,
                                color: '#fff',
                            },
                        },
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            show: false
                        },
                        axisTick: {
                            show: false
                        },

                    }],
                    series: [{
                        name: '条',
                        type: 'bar',
                        yAxisIndex: 0,
                        data: [40, 30, 20, 10],
                        barWidth: 10,
                        itemStyle: {
                            normal: {
                                barBorderRadius: 20,
                                color: function(params) {
                                    const num = myColor.length;
                                    return myColor[params.dataIndex % num]
                                },
                            }
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'inside',
                                formatter: '{c}%'
                            }
                        },
                    }, {
                        name: '框',
                        type: 'bar',
                        yAxisIndex: 1,
                        barGap: '-100%',
                        data: [100, 100, 100, 100],
                        barWidth: 15,
                        itemStyle: {
                            normal: {
                                color: 'none',
                                borderColor: '#00c1de',
                                borderWidth: 3,
                                barBorderRadius: 15,
                            }
                        }
                    }, ]
                })

            }
        }
    });
}

//
$(function() {
    staticClinicDeptadd(); // 新增病人记录
    selectWaitBedYear(); // 历年等床病人
    staticClinicDept(); // 科室就诊人数
    staticDentity(); // 就诊人员类型
    goinHospital(); // 科室出入院统计
    soldierType(); // 住院军种类型人员统计
})

